/*	2018.10.9   xiao  收货地址列表     */

//mui.init();
mui.init({
	beforeback: function() {
		//获得列表界面的webview  
		var list = plus.webview.currentWebview().opener();
		//触发列表界面的自定义事件（refresh）,从而进行数据刷新  
		mui.fire(list, 'refresh');
		//返回true，继续页面关闭逻辑  
		return true;
	}
});
//页面刷新
window.addEventListener('refresh', function(e) { //执行刷新 
	location.reload();
});
//获取本地信息
var my_phone = localStorage.getItem("my_phone");
var user_id = localStorage.getItem("user_id");

//返回订单
mui('body').on('tap', '.mui-back', function() {
	mui.back();
})

//新增地址
document.getElementById("add_address").addEventListener("tap", function() {
	mui.openWindow({
		url: "add_address.html",
		id: "add_address.html"
	});
});

//收货列表
AddressList();

function AddressList() {
	mui.ajax(order_url+'/api/user/userAddress', {
//		data: {
//			"phone": my_phone,
//			"sign": "string",
//			"user_id": user_id
//		},
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		timeout: 10000, //超时时间设置为10秒；
		success: function(data) {
			console.log(JSON.stringify(data));
			if(data.code == 10000) {				
				var html = "";
				if(data.resp.length==0){
					html='<div class="empty"> 暂无收货地址</div>';
				}
				else{
					for(var i = 0; i < data.resp.length; i++) {
						var status = data.resp[i].is_default; //默认
						var arr = [];
						arr = data.resp[i].address.split('-');
						arrStr = arr.join(" ");
						if(status == 1) {
							html += '<div class="address_list mui-radio mui-left">' +
								/*'<input name="radio" class="addresscheck" value="Item 1" type="radio" checked="checked">' +*/
								'<div class="address_name" id="' + data.resp[i].id + '">' +
								'<span class="address_name_size">' + data.resp[i].consigner + '</span>' +
								'<span class="address_name_size">' + data.resp[i].mobile + '</span>' +
								'<span class="address_sign">默认地址</span>' +
								'<p>' + arrStr + '</p>' +
								'<span class="mui-icon mui-icon-forward address_back"></span>' +
								'</div>' +
								'</div>';
						} else {
							html += '<div class="address_list mui-radio mui-left">' +
								/*'<input name="radio" class="addresscheck" value="Item 1" type="radio">' +*/
								'<div class="address_name" id="' + data.resp[i].id + '">' +
								'<span class="address_name_size">' + data.resp[i].consigner + '</span>' +
								'<span class="address_name_size">' + data.resp[i].mobile + '</span>' +
								'<p>' + arrStr + '</p>' +
								'<span class="mui-icon mui-icon-forward address_back"></span>' +
								'</div>' +
								'</div>';
						}
					}
				} 
				$(".address_content").html(html);

				//选择收货地址

			} else {
				mui.alert(data.msg);
			}
		},
		error: function(xhr, type, errorThrown) { //异常处理；
			console.log(type);
		}
	});
}

//查看收货地址信息
mui('body').on('tap', '.address_name', function() {
	var id = $(this).attr("id");
	mui.openWindow({
		url: "modify_address.html",
		id: "modify_address",
		extras: {
			address_id: id
		}
	});
});

//打开订单选择
/*$(document).on('tap', '.addresscheck', function() {
	var id = $(this).siblings().attr("id");
	var cityName = this.innerHTML;
	//获取父页面A.html
	var main = plus.webview.getWebviewById("Order_information.html");
	//自定义事件,事件名为changeCity
	console.log(id)
	mui.fire(main, 'changeaddressid', {
		cityName: id
	});
	//关闭子页面
	mui.back();
	 

})*/